package org.yn.base.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

/**
 * 邮件工具类
 *
 * @author chenkailiang
 * @date 2021/1/8
 */

@Slf4j
@Component
public class SendMailUtil {

    /**
     * 发件人
     */
    @Value("${spring.mail.username}")
    private String mailAddress;

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送普通文本邮件
     * @param from 发件人，如果未指定则使用默认配置的
     * @param to 收件人
     * @param cc 抄送
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    public void sendSimpleMail(String from,String[] to , String[] cc,String subject, String content) {

        SimpleMailMessage message = new SimpleMailMessage();
        //发件人
        String realFrom = from;
        if(StringUtils.isBlank(from)){
            realFrom = mailAddress;
        }
        message.setFrom(realFrom);
        //收信人
        message.setTo(to);
        //抄送
        message.setCc(cc);
        //主题
        message.setSubject(subject);
        //文本
        message.setText(content);
        mailSender.send(message);
        log.info("发送邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",realFrom, to,cc, subject);
    }

    /**
     * 发送html邮件
     * @param from 发件人
     * @param to 收件人
     * @param subject 主题
     * @param content 内容
     */
    public void sendHtmlMail(String from , String to, String subject, String content) {
        //使用MimeMessage，MIME协议
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        String realFrom = from;
        try {
            helper = new MimeMessageHelper(message, true);
            if(StringUtils.isBlank(from)){
                realFrom = mailAddress;
            }
            helper.setFrom(realFrom);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            mailSender.send(message);
            log.info("发送HTML邮件成功：发件人：{},收件人：{},主题：{}",realFrom, to, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送HTML邮件失败：发件人：{},收件人：{},主题：{}",realFrom, to, subject);
        }

    }

    /**
     * 发送带附件邮件
     * @param from 发件邮箱
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     * @param filePath 附件地址
     */
    public void sendAttachmentMail(String from,String[] to, String[] cc,String subject, String content, String filePath) {
        MimeMessage message = mailSender.createMimeMessage();
        MimeMessageHelper helper;
        String realFrom = from;
        try {
            //true代表支持多组件，如附件，图片等
            helper = new MimeMessageHelper(message, true);
            if(StringUtils.isBlank(from)){
                realFrom = mailAddress;
            }
            helper.setFrom(realFrom);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource file = new FileSystemResource(new File(filePath));
            String fileName = file.getFilename();
            //添加附件，可多次调用该方法添加多个附件
            helper.addAttachment(fileName, file);
            mailSender.send(message);
            log.info("发送带附件邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",realFrom, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送带附件邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",realFrom, to,cc, subject);
        }


    }

    /**
     * 发送带图片邮件
     * @param from 发件人
     * @param to 收件人
     * @param cc 抄送
     * @param subject 主题
     * @param content 内容
     * @param rscPath 图片路径
     * @param rscId 图片id
     */
    public void sendInlineResourceMail(String from,String[] to, String[] cc,String subject, String content, String rscPath, String rscId) {

        MimeMessage message = mailSender.createMimeMessage();
        String realFrom = from;
        MimeMessageHelper helper;
        try {
            helper = new MimeMessageHelper(message, true);
            if(StringUtils.isBlank(from)){
                realFrom = mailAddress;
            }
            helper.setFrom(realFrom);
            helper.setTo(to);
            helper.setSubject(subject);
            helper.setText(content, true);
            FileSystemResource res = new FileSystemResource(new File(rscPath));
            //重复使用添加多个图片
            helper.addInline(rscId, res);
            mailSender.send(message);
            log.info("发送带图片邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",realFrom, to,cc, subject);
        } catch (MessagingException e) {
            e.printStackTrace();
            log.error("发送带图片邮件成功：发件人：{},收件人：{},抄送：{},主题：{}",realFrom, to,cc, subject);
        }
    }

}
